package com.lanou.dllo.baisibudejie.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.dllo.baisibudejie.constant.Constant;
import com.lanou.dllo.baisibudejie.db.entity.Sql_MyNewsEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class MySQLiteMyNewsUtil {
    private SQLiteDatabase db;
    private Context context;

    public MySQLiteMyNewsUtil(Context c) {
        // TODO Auto-generated constructor stub
        context = c;
//        init();
        db = MySQLliteOpenHelper.getInstance(context, Constant.DATABASE_NAME, null, 1)
                .getWritableDatabase();
    }

//    private void init() {
//        MySQLliteOpenHelper helper = new MySQLliteOpenHelper(context, Constant.DATABASE_NAME, null, 1);
//        db = helper.getWritableDatabase();
//    }



    public void saveNewsDatasToDataBase(Sql_MyNewsEntity myNews) {
        ContentValues values = new ContentValues();
        values.put(Constant.COLUMN_MYNEWS_NAME, myNews.getName());
        values.put(Constant.COLUMN_MYNEWS_IMAGEURL, myNews.getImageUrl());
        db.insert(Constant.TABLE_NAME_MYNEWS, null, values);
    }

    public List<Sql_MyNewsEntity> queryMyNews() {
        String sql = " SELECT * FROM " + Constant.TABLE_NAME_MYNEWS;
        Cursor cursor = db.rawQuery(sql, null);
        List<Sql_MyNewsEntity> sql_myNewsEntities = new ArrayList<>();
        while (cursor.moveToNext()) {
            String imageUrl = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_MYNEWS_IMAGEURL));
            String name = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_MYNEWS_NAME));
            Sql_MyNewsEntity sql_myNewsEntity = new Sql_MyNewsEntity();
            sql_myNewsEntity.setName(name);
            sql_myNewsEntity.setImageUrl(imageUrl);
            sql_myNewsEntities.add(sql_myNewsEntity);
        }

        return sql_myNewsEntities;
    }



}
